package xyz.yq56.easytool.utils.http;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import javax.net.ssl.HttpsURLConnection;

import xyz.yq56.easytool.utils.exception.EasyToolException;
import xyz.yq56.easytool.utils.log.LogUtil;

/**
 * 支持https请求
 *
 * @author yi qiang
 * @date 2021/9/19 13:44
 */
public class HttpsUtil {

    private static final String HTTP_PREFIX = "http";

    private HttpsUtil() {
    }

    public static InputStream getInputStream(String url) {
        try {
            URLConnection connection = urlConnection(url);
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.connect();
            return connection.getInputStream();
        } catch (Exception e) {
            LogUtil.error("Connection Failed", e, null);
        }
        return null;
    }

    public static OutputStream getOutputStream(String url) {
        try {
            URLConnection connection = urlConnection(url);
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.connect();
            return connection.getOutputStream();
        } catch (Exception e) {
            LogUtil.error("Connection Failed", e, null);
        }
        return null;
    }

    public static URLConnection urlConnection(String url) {
        if (url.startsWith(HTTP_PREFIX)) {
            return httpURLConnection(url);
        }
        return httpsURLConnection(url);
    }

    public static HttpURLConnection httpURLConnection(String urlStr) {
        try {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            return conn;
        } catch (Exception e) {
            LogUtil.error("httpURLConnection", e, null);
            throw new EasyToolException(e);
        }
    }

    public static HttpsURLConnection httpsURLConnection(String urlStr) {
        try {
            URL url = new URL(urlStr);
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setHostnameVerifier(UnSafeX509TrustManager.trustAnyHostnameVerifier());
            conn.setSSLSocketFactory(UnSafeX509TrustManager.sslSocketFactory());
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            return conn;
        } catch (Exception e) {
            throw new EasyToolException(e);
        }
    }

}
